// pages/userset/userset.js
const api = require("../../utils/interface.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{} 
  },
  gosave(){
    if (!this.data.userInfo) {
      wx.redirectTo({
        url: '/pages/login/login?page=pages/userset/userset&state=1',
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/userset/setsave/setsave'
    })
  },
  goclick() {
    wx.navigateTo({
      url: '/pages/aboutUs/aboutUs'
    })
  },
  gounsave(){
    wx.showModal({
      title: '提示',
      content: "确认清除缓存",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.clearStorageSync()
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      }
    })
  },
  onLoad: function (options) {
    this.getUserinfo()
  },
  // 获取用户头像昵称
  getUserinfo(e) {
    let user = wx.getStorageSync('userInfo')
    if (!user){
      this.setData({
        userInfo:null
      })
      return false
    }
    api.getMemberInfo({
      id: user.id
    }).then(res => {
      this.data.userInfo = res.data.result
    })
  },
  // 退出登陆
  loginOut(){
    let openid = wx.getStorageSync("openid")
    
      wx.showModal({
        title: '提示',
        content: '是否要退出当前账号',
        success(res){
          if (res.confirm) {
            api.loginOUt({ openId: openid }).then(res => {
              wx.clearStorageSync()
              wx.switchTab({
                url: '/pages/index/index',
              })
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    
  },
  
})